package com.example.seakdreamandroid.sleep.adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.sleep.AlarmClockAndPrompt;
import com.example.seakdreamandroid.sleep.AlarmItemDetail;
import com.example.seakdreamandroid.sleep.NapActivity;
import com.example.seakdreamandroid.sleep.entity.ClockInfo;
import com.example.seakdreamandroid.sleep.util.MediaUtil;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ClockAdapter extends BaseAdapter {
    private Context mContext;
    private List<ClockInfo> clockInfos;
    private int layoutId;
    private String str;
    private FormBody body;


    public ClockAdapter(Context context, List<ClockInfo> clockInfos,int layoutId) {
        this.mContext = context;
        this.clockInfos = clockInfos;
        this.layoutId = layoutId;
    }
    @Override
    public int getCount() {
        if (clockInfos != null) {
            return clockInfos.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (clockInfos != null) {
            return clockInfos.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(layoutId, null);
            viewHolder = new ViewHolder();
            viewHolder.memo = view.findViewById(R.id.tv_remarks);
            viewHolder.repeatSituation = view.findViewById(R.id.tv_repeat);
            viewHolder.time = view.findViewById(R.id.tv_time);
            viewHolder.ifOpen = view.findViewById(R.id.if_open);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.time.setText(clockInfos.get(i).getTime());
        if (clockInfos.get(i).getMemo().equals("") ) {
            viewHolder.memo.setText("自定义闹钟");
        } else {
            viewHolder.memo.setText(clockInfos.get(i).getMemo());
        }
        viewHolder.repeatSituation.setText(clockInfos.get(i).getRepeatSituation());
        int ifOpen = clockInfos.get(i).getIfOpen();
        if (ifOpen == 1) {
            viewHolder.ifOpen.setChecked(true);
        } else {
            viewHolder.ifOpen.setChecked(false);
        }
        if ((clockInfos.get(i).getMemo().equals("就寝提示") || clockInfos.get(i).getMemo().equals("小憩提示") || clockInfos.get(i).getMemo().equals("睡前仪式提醒"))) {
            viewHolder.ifOpen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        clockInfos.get(i).setIfOpen(1);
                    } else {
                        clockInfos.get(i).setIfOpen(0);
                    }
                }
            });
        } else {
            viewHolder.ifOpen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        // 开启
                        //1、更改数据库中的ifOpen
                        clockInfos.get(i).setIfOpen(1);
                        body= new FormBody.Builder()
                                .add("userId",clockInfos.get(i).getUserId())
                                .add("clockId",clockInfos.get(i).getClockId())
                                .add("time", clockInfos.get(i).getTime())
                                .add("memo",clockInfos.get(i).getMemo())
                                .add("repeatSituation",clockInfos.get(i).getRepeatSituation())
                                .add("ifOpen", String.valueOf(clockInfos.get(i).getIfOpen()))
                                .build();
                        postAsync(body);
                    } else {
                        // 关闭
                        //1、更改数据库中的ifOpen
                        clockInfos.get(i).setIfOpen(0);
                        body= new FormBody.Builder()
                                .add("userId",clockInfos.get(i).getUserId())
                                .add("clockId",clockInfos.get(i).getClockId())
                                .add("time", clockInfos.get(i).getTime())
                                .add("memo",clockInfos.get(i).getMemo())
                                .add("repeatSituation",clockInfos.get(i).getRepeatSituation())
                                .add("ifOpen", String.valueOf(clockInfos.get(i).getIfOpen()))
                                .build();
                        postAsync(body);
                    }
                }
            });
        }
        return view;
    }

    private void postAsync(FormBody body) {
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建请求对象（Request）
        Request request = new Request.Builder()
                //设置
                .url("http://192.168.1.3:8080/clock/editClock?")
                .post(body)//设置请求方式为post，添加了请求体
                //创建Request对象
                .build();
        //发送请求，获取响应
        //(一个call对象对应一个response)
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("okHttp","onfilure");
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());
                //修改用户界面，需要使用Handler或RunOnUiThread
            }
        });
    }


    private static class ViewHolder{
        //每一个item中的控件
        TextView memo;
        TextView repeatSituation;
        TextView time;
        ToggleButton ifOpen;
    }
}
